﻿using System;
using System.Drawing;

namespace Calendar.Domain
{

    public class WorkHandler
    {
        public AddWorkHandler AddWorkEvent = null;
        public AddDateWorkHandler AddDateWorkEvent = null;
        public EmptyWorkHandler EmptyWorkEvent = null;
        public TodoList _todoList;

        public WorkHandler()
        {
            _todoList = new TodoList();
            _todoList.AddWorkEvent += AddWorkInvoke;
            _todoList.AddDateWorkEvent += AddDateWork;
            _todoList.EmptyWorkEvent += AddEmptyWork;
        }

        public void LoadWork()
        {
            _todoList.LoadWork();
        }

        public void SaveWork()
        {
            _todoList.SaveWork();
        }

        public void AddEmptyWork()
        {
            EmptyWorkEvent.Invoke();
        }

        public void AddWork(string workName, DateTime startDateTime, DateTime deadlineDateTime, int emergancyLevel, int progress)
        {
            _todoList.AddWork(workName, startDateTime, deadlineDateTime, emergancyLevel, progress);
            SaveWork();
        }

        public void AddWorkInvoke(int id, string name, DateTime deadline)
        {
            AddWorkEvent.Invoke(id, name, deadline);
        }

        public void AddDateWork(int id, string name, DateTime deadline)
        {
            AddDateWorkEvent.Invoke(id, name, deadline);
        }

        public void DeleteWork(int id)
        {
            _todoList.DeleteWork(id);
            SaveWork();
            LoadWork();
        }

        public string FormatDate(string date)
        {
            string[] s = date.Split('/');
            s[1] = AddZero(s[1]);
            s[2] = AddZero(s[2]);
            return String.Join("/", s);
        }

        private string AddZero(string s)
        {
            if (Convert.ToInt32(s) < 10)
            {
                string temp = "0" + s;
                s = temp;
            }
            return s;
        }

        public string GetName(int id)
        {
            return _todoList.GetName(id);
        }

        public DateTime GetDeadline(int id)
        {
            return _todoList.GetDeadline(id);
        }

        public int GetProgress(int id)
        {
            return _todoList.GetProgress(id);
        }

        public int GetEmergencyLevel(int id)
        {
            return _todoList.GetEmergencyLevel(id);
        }

        public DateTime GetStartTime(int id)
        {
            return _todoList.GetStartTime(id);
        }

        public bool IsChecked(int id)
        {
            if (GetProgress(id)==100)
            {
                return true;
            }
            return false;
        }

        public Color GetNodeColor(int id)
        {
            if (GetProgress(id) == 100)
            {
                return Color.Gray;
            }
            return Color.Black;
        }

        public FontStyle GetNodeFontStyle(int id)
        {
            if (GetProgress(id) == 100)
            {
                return FontStyle.Strikeout;
            }
            return FontStyle.Regular;
        }

        //public void SetFullProgress(int id)
        //{
        //    _todoList.SetFullProgress(id);
        //}

        public void SetName(int id, string name)
        {
            _todoList.SetName(id, name);
            SaveWork();
        }

        public void SetStartTime(int id, DateTime startTime)
        {
            _todoList.SetStartTime(id, startTime);
            SaveWork();
        }

        public void SetDeadline(int id, DateTime deadline)
        {
            _todoList.SetDeadline(id, deadline);
            SaveWork();
        }

        public void SetEmergancyLevel(int id, int emergancyLevel)
        {
            _todoList.SetEmergancyLevel(id, emergancyLevel);
            SaveWork();
        }

        public void SetProgress(int id, int persentage)
        {
            _todoList.SetProgress(id, persentage);
            SaveWork();
        }
    }
}
